register.js ➔ register   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 30
rs 8
c 0
b 0
f 0
cc 7
1
var express = require('express');
2
var router = express.Router();
3
const sqlite3 = require('sqlite3').verbose();
0 ignored issues
show
Unused Code introduced by
The constant sqlite3 seems to be never used. Consider removing it.
Loading history...
4
// const db = new sqlite3.Database('./db/texts.sqlite');
5
const db = require("../db/database.js");
6
const bcrypt = require('bcryptjs');
7
8
9
router.get('/', function(req, res, next) {
0 ignored issues
show
Unused Code introduced by
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
10
    const data = {
11
        data: {
12
            msg:  "Register a user"
13
        }
14
    };
15
16
    res.json(data);
17
});
18
19
router.post("/", (req, res) => {
20
    register(res, req.body);
21
22
    // res.status(201).json({
23
    //     data: {
24
    //         msg: "Got a POST request"
25
    //     }
26
    // });
27
});
28
29
function register(res, body) {
30
    const email = body.email;
31
    const password = body.password;
32
33
    if (!email || !password) {
34
        return res.status(401).json({
35
            errors: {
36
                status: 401,
37
                source: "/register",
38
                title: "Email or password missing",
39
                detail: "Email or password missing in request"
40
            }
41
        });
42
    }
43
    bcrypt.hash(password, 10, function(err, hash) {
44
45
        db.run("INSERT INTO users (email, password) VALUES (?, ?)",
46
            email,
47
            hash, (err) => {
48
                if (err) {
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
49
                    // console.log(err);
50
                }
51
                return res.status(201).json({
52
                    data: {
53
                        message: "User successfully registered."
54
                    }
55
                });
56
            });
57
    });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
58
}
59
60
module.exports = router;
61